package vapor.aquila.client.ui;

import com.google.gwt.user.client.ui.*;
import vapor.aquila.client.game.*;
import gwt.canvas.client.*;

/**
 * A WorldViewController attaches a World to a Canvas
 * @author Travis Gockel
 */
@SuppressWarnings("deprecation")
public class WorldViewController implements MouseListener, MouseWheelListener
{
	private Canvas m_canvas;
	private int m_worldOffsetLat, m_worldOffsetLong;
	private int m_tileSize;
	private World m_world;
	
	public WorldViewController(World world, Canvas canvas)
	{
		m_worldOffsetLat = 0;
		m_worldOffsetLong = 0;
		m_tileSize = 64;
		setWorld(world);
		setCanvas(canvas);
	}
	
	//	ACCESSORS	/////////////////////////////////////////////////////////
	/**
	 * Gets the canvas this WVC is drawing to. 
	 */
	public Canvas getCanvas() { return m_canvas; }
	
	/**
	 * Sets the Canvas this WVC draws to.
	 * @param canvas The canvas to set to begin drawing for.
	 */
	public void setCanvas(Canvas canvas)
	{
		if (m_canvas != null)
		{
			m_canvas.removeMouseListener(this);
			m_canvas.removeMouseWheelListener(this);
		}
		m_canvas = canvas;
		if (canvas != null)
		{
			canvas.setBackgroundColor("#000000");
			canvas.addMouseListener(this);
			canvas.addMouseWheelListener(this);
			draw();
		}
	}
	
	public int getTileSize(){ return m_tileSize; }
	
	public void setTileSize(int tileSize)
	{
		m_tileSize = tileSize <= 1 ? 1 : tileSize;
		draw();
	}
	
	public int getWorldOffsetLatitude() { return m_worldOffsetLat; }
	
	public void setWorldOffsetLatitude(int lat)
	{
		int width = (m_world != null) ? m_world.getMapSizeLatitude() : 100;
		m_worldOffsetLat = (lat % width + width) % width;
		draw();
	}
	
	public int getWorldOffsetLongitude() { return m_worldOffsetLong; }
	
	public void setWorldOffsetLongitude(int lon)
	{
		int height = (m_world != null) ? m_world.getMapSizeLongitude() : 100;
		m_worldOffsetLat = (lon % height + height) % height;
		draw();
	}
	
	public World getWorld() { return m_world; }
	
	public void setWorld(World world)
	{
		m_world = world;
		draw();
	}
	
	//	DRAWING FUNCTIONS	/////////////////////////////////////////////////
	public void draw()
	{
		//	if there is nothing to draw on, then quit
		if (m_canvas == null)
			return;
		
		if (m_world == null)
		{
			m_canvas.clear();
		}
		else
		{
			//	determine the amount of tiles to draw
			int width = m_canvas.getWidth() / m_tileSize,
			    height = m_canvas.getHeight() / m_tileSize;
			showAlert("" + width + ", " + height);
			
			for (int x = 0; x < width; x++)
			{
				for (int y = 0; y < height; y++)
				{
					int xpos = x * m_tileSize,
					    ypos = y * m_tileSize;
					Tile tile = m_world.getMapTile(m_worldOffsetLat + x, m_worldOffsetLong + y);
					Image img = (tile == null) ? ImageFactory.grass().createImage() : tile.getImage();
					m_canvas.scale((float)img.getWidth() / m_tileSize,
					               (float)img.getHeight() / m_tileSize);
					m_canvas.setFillStyle("#990000");
					m_canvas.fillRect(xpos, ypos, m_tileSize, m_tileSize);
					m_canvas.drawImage(img, xpos, ypos);
				}
			}
			
			m_canvas.scale(1.0, 1.0);
		}
	}

	//	ACTIONS		/////////////////////////////////////////////////////////
	@Override
	public void onMouseDown(Widget sender, int x, int y)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMouseEnter(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMouseLeave(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMouseMove(Widget sender, int x, int y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMouseUp(Widget sender, int x, int y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMouseWheel(Widget sender, MouseWheelVelocity velocity) {
		// TODO Auto-generated method stub
	}
	
	public native void showAlert(String str)/*-{
		alert(str);
	}-*/;
}
